<template>
	<view>
		<view style="margin-bottom: 50rpx;">
			<uni-list style="width: 100%;" >
				<uni-list-item
				title= "创建团队" 
				clickable
				:showArrow= "true" 
				@click= "goCreatTeam" />
				<uni-list-item
				title= "参数设置" 
				clickable
				:showArrow= "true" 
				@click= "goConfig" />
			</uni-list>
		</view>
		<uni-section title="团队" type="line"></uni-section>
		<view style="width: 100%; margin-top: 20rpx;" v-if="team_count > 0">
			<uni-list style="width: 100%;">
				<uni-swipe-action>
					<uni-swipe-action-item v-for="item in listData"
					:key="item.id"
					:right-options="options" 
					:show="isOpen" 
					:auto-close="true" 
					@change="change" 
					@click="bindClick($event,item)">
						<view class="content-box">
							<uni-list-item 
							:key="item.id" 
							:thumb="item.avatarUrl == '' ? defaultAvatar : item.avatarUrl"
							thumb-size="lg"
							:title="item.name" 
							:note="item.creatTime" 
							showArrow
							rightText="详细" 
							clickable 
							@click="goTeamManage(item.id)"></uni-list-item>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</uni-list>
		</view>
		<view style="width: 100%; margin-top: 20rpx;" v-else>
			<text class="tips" v-if="listData.length <= 0">你还没有创建或加入任何团队</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				defaultAvatar: '/static/defaultAvatar.png',
				user: [],
				team_count: 1,
				page: 1,
				isLogin: false,
				isOpen: "none",
				listData: [],
				loadMoreText: "加载中...",
				showLoadMore: false,
				options: [{
						text: '编辑',
						style: {
							backgroundColor: '#007aff'
						}
					},
					{
						text: '删除',
						style: {
							backgroundColor: '#dd524d'
						}
					}
				],
			}
		},
		onShow() {
			if(!this.$dry.isLogin()) {
				uni.showModal({
					title: "信息",
					content: "需要登录后查看，去登录",
					showCancel: false,
					confirmText: "确定",
					success: function (res) {
						if (res.confirm) {
							uni.switchTab({
								url: '../user/user',
							})
						}
					}
				});
			}
			this.user = this.$dry.get("user");
			this.initData();
		},
		methods: {
			initData(){
				this.page = 1;
				this.listData = [];
				this.getList();
			},
			getList(){
				var token = this.$dry.getToken();
				var data = [];
				if(token){
				    data = this.$request.run("listTeam",token);
				    uni.request({
				    	url: data.url,
				    	method: data.method,
				    	header: data.header,
				    	data: {
							isCreator: false
						},
				    	success: data => {
							this.team_count = data.data.length;
							this.listData = data.data;
						}
					})
				}
			},
			change(e) {
				this.isOpen = e;
				//console.log('返回：', e);
			},
			bindClick(e,data) {
				var that = this;
				if(data.owner != this.user.userId){
					uni.showToast({
						title: "对不起，只有团队创建者才能编辑",
						duration: 1000
					})
				}
				else{
					if(e.index == 0){
						uni.setStorageSync("team_data",data);
						uni.navigateTo({
							url: '../new/team-new-edit',
						})
					}
					else {
						uni.showModal({
							title: "信息",
							content: "确定要删除" + data.name + "吗",
							showCancel: true,
							confirmText: "确定",
							success: function (res) {
								if (res.confirm) {
									var token = that.$dry.getToken();
									var requestData = {
										"ids": data.id
									}
									that.$request.delete("deleteBetchTeams",requestData,token);
									that.initData();
								}
							}
						});
					}
				}
			},
			goCreatTeam(){
				uni.navigateTo({
					url: '../new/team-new-edit',
				})
			},
			goTeamManage(id){
				uni.navigateTo({
					url: 'manage?id=' + id,
				})
			},
			goConfig() {
				uni.navigateTo({
					url: 'config'
				})
			},
		}
	}
</script>

<style>
	.tips{
		padding:10rpx 20rpx;
		text-align: left;
		font-size: 24rpx;
		color: #0077AA;
	}
</style>
